* This file combines the stata waybill files, drops uninteresting STCC codes, compresses the data,
* and performs several other operations to get the data in a more usable form. 

clear matrix
drop _all
set more off
set mem 10g

global pt "Your File Path Here"

* Combine files
forv x=2009/2014{
append using "$pt\wb`x'"
}

* Drop obs with error codes
drop if drop == 1

* Save all goods for all states
compress
save "$pt\AllGoods.dta", replace

* Make a file of total rail traffic by RR by month to use as control
use "$pt\AllGoods.dta", clear
* Drop grain and oil
gen grain_oil = 0
replace grain_oil = 1 if STCC == 1311110 /* 7-digit STCC's for oil and petroleum */
replace grain_oil = 1 if STCC == 4910165
replace grain_oil = 1 if STCC == 4910191
replace grain_oil = 1 if STCC5 >= 1130 & STCC5 <= 1139 /* 5-digit STCC's 0113X are "grain" */ 
replace grain_oil = 1 if STCC5 == 1144 /* Soy */
drop if grain_oil == 1
sort WYEAR WMO ORR_A 
collapse (sum) Cars, by(WYEAR WMO ORR_A)
rename Cars TotTraffic
sort WYEAR WMO ORR_A 
save "/Users/Jon/Dropbox/Rail and Oil/Data/MonthlyCarloads.dta", replace 

* Make a file of total rail traffic by RR by day to use as control
use "$pt\AllGoods.dta", clear
* Drop grain and oil
gen grain_oil = 0
replace grain_oil = 1 if STCC == 1311110 /* 7-digit STCC's for oil and petroleum */
replace grain_oil = 1 if STCC == 4910165
replace grain_oil = 1 if STCC == 4910191
replace grain_oil = 1 if STCC5 >= 1130 & STCC5 <= 1139 /* 5-digit STCC's 0113X are "grain" */ 
replace grain_oil = 1 if STCC5 == 1144 /* Soy */
drop if grain_oil == 1
sort WYEAR WMO WDAY ORR_A 
collapse (sum) Cars, by(WYEAR WMO WDAY ORR_A)
rename Cars TotTraffic
sort WYEAR WMO WDAY ORR_A 
save "/Users/Jon/Dropbox/Rail and Oil/Data/DailyCarloads.dta", replace 

* Keep oil STCC's (7-digit) and grain STCC's
gen grain_oil = 0
replace grain_oil = 1 if STCC == 1311110 /* 7-digit STCC's for oil and petroleum */
replace grain_oil = 1 if STCC == 4910165
replace grain_oil = 1 if STCC == 4910191
replace grain_oil = 1 if STCC5 >= 1130 & STCC5 <= 1139 /* 5-digit STCC's 0113X are "grain" */ 
replace grain_oil = 1 if STCC5 == 1144 /* Soy */
keep if grain_oil == 1

drop _merge

* Compress and save
compress
save "/Users/Jon/Dropbox/Rail and Oil/Data/CrudeOilGrain.dta", replace

* Make a file for all goods from ND
* Combine files
forv x=2009/2014{
append using "$pt\wb`x'"
}

* Drop obs with error codes
drop if drop == 1

* Keep ND
keep if O_ST == "ND"

* Compress and save
compress
save "/Users/Jon/Dropbox/Rail and Oil/Data/AllGoodsND.dta", replace
